/*Libreria para protocolo SPI por hardware para HiTech*/

#define SPI_MASTER			0x20
#define SPI_SLAVE			0x24
#define SPI_SS_DISABLED      1
#define SPI_IDLE_L           0
#define SPI_IDLE_H           0x10
#define SPI_CLK_DIV4         0
#define SPI_CLK_DIV16        1
#define SPI_CLK_DIV64        2
#define SPI_CLK_T2           3
#define SPI_SAMPLE_AT_END   		0x8000
#define SPI_XMIT_IDLE_TO_ACTIVE   	0x4000

void setup_spi(unsigned int config)
{
TRISC4 = 1;
SSPSTAT = config/256;
SSPCON = config &0xFF;
if (SSPM2 == 0) TRISC3 = 0;
else
{
TRISC3 = 1;
if (SSPM0 == 0) TRISA5 = 1;
}
}

unsigned char write_spi (unsigned char spi_data_out)
{
TRISC5 = 0;
SSPBUF = spi_data_out;
 while (SSPIF==0); 
SSPIF = 0; 
return SSPBUF;
}

unsigned char read_spi (void)
{
TRISC5 = 1;  
SSPBUF = 16;
while (SSPIF==0); 
SSPIF = 0; 
return SSPBUF;
}

void write_spi_int (unsigned char spi_data_out)
{
TRISC5 = 0;
SSPBUF = spi_data_out;
}


void read_spi_int (void)
{
TRISC5 = 1;
SSPBUF = 0;
}
